<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="tag" uri="http://www.springframework.org/tags" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core_rt" %>

<% String perms = request.getParameter("perms"); %>
<div id="fb-root"></div>
<script>
    //#################################ACCESS_TOKEN#######################
    var accessToken;
    //####################################################################
    // Load the SDK Asynchronously
    (function(d){
        var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0];
        if (d.getElementById(id)) {return;}
        js = d.createElement('script'); js.id = id; js.async = true;
        js.src = "//connect.facebook.net/pl_PL/all.js";
        ref.parentNode.insertBefore(js, ref);
    }(document));

    // Init the SDK upon load
    window.fbAsyncInit = function() {
    FB.init({
        appId      : '280656762009591', // App ID
        //channelUrl : '//'+window.location.hostname+'/channel', // Path to your Channel File
        channelUrl: 'http://localhost:8080/channel.html',
        status     : true, // check login status
        cookie     : true, // enable cookies to allow the server to access the session
        xfbml      : true  // parse XFBML
    });


    // listen for and handle auth.statusChange events
    FB.Event.subscribe('auth.statusChange', function(response) {
        if (response.authResponse) {
        // user has auth'd your app and is logged into Facebook
        
        //####################################################################
        // w tej zmiennej zapamietywany jest token
        localStorage.setItem("token", response.authResponse.accessToken);
        //####################################################################
        
        FB.api('/me', function(me){
            if (me.name) {
            document.getElementById('auth-displayname').innerHTML = me.name;
            }
        })
        document.getElementById('auth-loggedout').style.display = 'none';
        document.getElementById('auth-loggedin').style.display = 'block';
        } else {
        // user has not auth'd your app, or is not logged into Facebook
        document.getElementById('auth-loggedout').style.display = 'block';
        document.getElementById('auth-loggedin').style.display = 'none';
        }
    });

    // respond to clicks on the login and logout links
    document.getElementById('auth-loginlink').addEventListener('click', function(){
        FB.login(function(response) {
        if (response.authResponse) {
            // user is logged in and granted some permissions.
            accessToken = response.authResponse.accessToken;
            // alert("Twój access token to "+ accessToken);
        } else {
            // User cancelled login or did not fully authorize.
        }
        }, {scope:'<%=perms%>'});  
    });
    document.getElementById('auth-logoutlink').addEventListener('click', function(){
        FB.logout();
    }); 
    } 
</script>
${param.parametr}
<div id="auth-status">
    <div id="auth-loggedout">
        <a href="#" id="auth-loginlink"><tag:message code="login.login"/></a>
    </div>
    <div id="auth-loggedin" style="display:none">
        Zalogowany jako: <span id="auth-displayname"></span>  
        (<a href="#" id="auth-logoutlink">logout</a>)
    </div>
    <div id="app-logout">
        <tag:message code="login.logoutHeader"/>
        <a href="<c:url value="/j_spring_security_logout"/>"><tag:message code="login.logout"/></a>
    </div>
</div>

    